package me.jessyan.mvparms.demo.mvp.ui.activity.car;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.jess.arms.base.BaseActivity;
import com.jess.arms.base.DefaultAdapter;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.StautBarUtils;
import com.jess.arms.widget.CustomPopupWindow;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import butterknife.BindView;
import butterknife.OnClick;
import me.jessyan.mvparms.demo.R;
import me.jessyan.mvparms.demo.di.component.car.DaggerPayCostComponent;
import me.jessyan.mvparms.demo.di.module.car.PayCostModule;
import me.jessyan.mvparms.demo.mvp.contract.car.PayCostContract;
import me.jessyan.mvparms.demo.mvp.presenter.car.PayCostPresenter;
import me.jessyan.mvparms.demo.mvp.ui.adapter.SelectTicketAdapter;
import me.jessyan.mvparms.demo.mvp.ui.widget.CustomToolbar;
import me.jessyan.mvparms.demo.utils.PerfectClickListener;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * 停车缴费
 */
public class PayCostActivity extends BaseActivity<PayCostPresenter> implements PayCostContract.View ,PopupWindow.OnDismissListener{

    @BindView(R.id.toolbar)
    CustomToolbar toolbar;
    @BindView(R.id.parking_coupon_tv)
    TextView parkingCouponTv;
    @Inject
    @Named("payTimeoutPop")
    CustomPopupWindow payTimeoutPop;

    @Inject
    @Named("selectParkingTicketPop")
    CustomPopupWindow selectParkingTicketPop;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        //状态栏全屏


        DaggerPayCostComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .payCostModule(new PayCostModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_pay_cost; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        StautBarUtils.setStatusMode(this, false, true);
        StautBarUtils.setBarPadding(findViewById(R.id.cl));

        toolbar.setListener(new CustomToolbar.ToolbarListener() {
            @Override
            public void onLeftBack() {
                killMyself();
            }

            @Override
            public void onRightBack() {

            }
        });

        if(selectParkingTicketPop!=null){
            selectParkingTicketPop.setOnDismissListener(this);
        }
        if(payTimeoutPop!=null){
            payTimeoutPop.setOnDismissListener(this);
        }
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }


    @OnClick({R.id.parking_coupon_tv, R.id.immediate_payment_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.parking_coupon_tv:
                selectParkingTicketPop.showBottom();
                backgroundAlpha(0.7f);
                break;
            case R.id.immediate_payment_tv:
                payTimeoutPop.show();
                backgroundAlpha(0.7f);
                break;
            default:
        }
    }

    @Override
    public PayCostActivity getActivity() {
        return this;
    }

    @Override
    public void dismissPop() {
        if (selectParkingTicketPop != null && selectParkingTicketPop.isShowing()) {
            selectParkingTicketPop.dismiss();
        }
        if (payTimeoutPop != null && payTimeoutPop.isShowing()) {
            payTimeoutPop.dismiss();
        }
    }


    /**
     * 优惠券的pop 初始化
     * @param contentView
     */
    @Override
    public void choiceTicketPopInit(View contentView) {
        List<String>  tickets = new ArrayList<>();
        tickets.add("3元停车优惠券");
        tickets.add("5元停车优惠券");
        tickets.add("不使用优惠券");

        RecyclerView recyclerView = contentView.findViewById(R.id.recyclerView);
        LinearLayoutManager lr = new LinearLayoutManager(contentView.getContext(),LinearLayoutManager.VERTICAL,false);
        recyclerView.setLayoutManager(lr);
        ArmsUtils.configRecyclerView(recyclerView,lr);


        SelectTicketAdapter selectAdapter= new SelectTicketAdapter(tickets);

        recyclerView.setAdapter(selectAdapter);
        selectAdapter.setOnItemClickListener(new DefaultAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int viewType, Object data, int position) {
                selectAdapter.setSelected(position);
                parkingCouponTv.setText(tickets.get(position));
            }
        });

        contentView.findViewById(R.id.cancel_tv).setOnClickListener(new PerfectClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                dismissPop();
            }
        });
    }

    @Override
    public void onDismiss() {
        backgroundAlpha(1);
    }
}
